﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkStation.Logic;
using WorkStation.UI;

namespace WorkStation.Common
{
    public class GlobalVariable
    {
        public static bool FrontAllowPlateOut = false;
        public static bool TestMesEnable1 { get; set; }
        public static bool TestMesEnable2 { get; set; }
        public static string MesConfig { get; set; } = string.Empty;
        public static bool MirrorCheckFinish { get; set; }
        public static bool MirrorCheckOK;
        public static bool IsSystemRunning { get; set; }
        public static bool IsSystemErr { get; set; }
        public static bool IsSystemAlreadyHome { get; set; }
        public static bool IsSystemHoming { get; set; }
        public static bool NeedRecycleEmptyBoatTray1 { get; set; }
        public static bool NeedRecycleEmptyBoatTray2 { get; set; }
        public static bool NeedRecycleEmptyBoatTrayFinish1 { get; set; }
        public static bool NeedRecycleEmptyBoatTrayFinish2 { get; set; }
        public static bool IsClearLater { get; set; }
        public static bool IsClearNow { get; set; }
        public static bool IsClearLater2 { get; set; }
        public static bool IsClearNow2 { get; set; }
        public static bool IsClearDownTray { get; set; }
        public static bool IsClearDownTrayFinish { get; set; }
        public static bool IsClearDownTray2 { get; set; }
        public static bool IsClearDownTrayFinish2 { get; set; }
        public static bool IsClearFinish { get; set; }
        public static bool FrontClearFinish { get; set; }
        public static bool BackClearFinish { get; set; }
        public static bool IsOnlineRetest { get; set; }
        public static WorkMode MachineWorkMode { get; set; }
        public static WorkSpeed MachineWorkSpeed { get; set; }
        public static UserInfo UserRole { get; set; } = new UserInfo();
        public static bool[] RobotStatus { get; set; } = new bool[41];
        public static bool[] RobotControlStatus { get; set; } = new bool[24];
        public static bool[] RobotStatus2 { get; set; } = new bool[41];
        public static bool[] RobotControlStatus2 { get; set; } = new bool[24];
        public static bool IsRobotDryRun;
        public static bool UnloadCylinderStatus { get; set; }
        public static bool BufferCylinderStatus { get; set; }
        public static bool[] PCWritePLCData { get; set; } = new bool[30];
        public static bool[] PCReadPLCData { get; set; } = new bool[20];
        public static bool[] PCReadPLCDataSecond { get; set; } = new bool[4];
        public static int PLCStatusData;
        public static bool[] PLCErrorCode { get; set; } = new bool[84];
        public static bool[] PLCWarnCode { get; set; } = new bool[12];
        public static object LockUpdate { get; set; } = new object();
        public static object TransferLock { get; set; } = new object();
        public static List<StationTestResult> StationResults;
        public static MachineData CurrentMachineData;
        public static object SaveConfigLock = new object();
        public static ThreeColorLight ColorLight;
        public static SerializableDictionary<string, int> AlarmDict;
        public static bool IsCheckNGTray = false;
        public static bool IsCheckBarcodeNGTray = false;
        public static bool NGTrayChangeSuccessful = false;
        public static bool HasNotifyRobotSwitch = false;

        public static int GrrCount = 0;
        public static int GrrTotalCountSingle = 6;
        public static Dictionary<int, int> TesterNo2GrrTimesSingle = new Dictionary<int, int>();
        //blic static int GrrTotalTimesSingle = 5;
        public static int GrrDelayTime = 30000;
        public static bool IsBGrr = false;
        public static bool FeedGrrFinish = false;
        public static bool TotalGrrFinish = false;
        public static bool IsGrrMode = false;
        public static bool IsCosmeticMode = false;
        public static List<int> CosmeticFinishTesterIndex = new List<int>();

        public static bool IsSafeDoorCheck = true;
        public static bool isSafeDoorOpen = false;
        public static bool isSafeLightOpenFront = false;
        public static bool isSafeLightOpenBack = false;
        public static string[] RobotCylinderWarnContent = new string[] { "",""};

        public static bool UseTwiceHandshake = true;

        public static int UPHCalculateCount = 0;
        public static string CurrentLeftTcpMessage;
        public static object LeftTcpLock = new object();
        public static string CurrentRightTcpMessage;
        public static object RightTcpLock = new object();

        public static Product2Mirror P2M = new Product2Mirror();

        public static bool NowSpotCheckMode;

        //是否可以点检
        public static bool IsReadyForCheck;
        public static bool LeftFinishSpotCheck;
        public static bool RightFinishSpotCheck;

        public static bool ClearBufferNow;
        public static bool ClearBufferFinish;

        public static bool TrayOut;
        public static bool TrayOutFinish;

        public static bool IsEcoMode = false;
        public static bool IsFirstTimeHomeFinish = false;
        public static bool RobotCommDisable = false;

        public static TesterLogic TesterModule1Logic;
        public static TesterLogic TesterModule2Logic;
        public static TesterLogic TesterModule3Logic;
        public static TesterLogic Tester1Logic;
        public static TesterLogic Tester2Logic;
        public static TesterLogic Tester3Logic;

        //ALT
        //上料待测盘 30 * 4
        public static Tray PrecisionTray = new Tray(1, 4);
        public static Tray BarcodeNGTray = new Tray(30, 1);
        //第一个头4个吸嘴
        public static Tray Head2RobotTray = new Tray(2, 2);
        public static Tray Head3RobotTray = new Tray(2, 2);
        public static Tray OKTray = new Tray(30, 2);
        public static Tray NGTray = new Tray(30, 4);
        public static Tray BufferTray = new Tray(2, 2);
        public static List<Tray> TesterTrays = new List<Tray>();

        //精定位区的锁
        public static object PrecisionAreaLock = new object();
        //Tester1区的锁
        public static object Tester1AreaLock = new object();
        public static object NGTrackLock = new object();
        public static object OKTrayLock = new object();
        public static bool AllowRecycleNGTray;

        public static bool NGButtonSimulatePress = false;


        public static CaptureTrayLogic CaptureTrayFront;
        public static CaptureTrayLogic CaptureTrayBack;
        public static PLCStatusLogic PLCStatus;
        public static PrecisionLogic PrecisionFront;
        public static PrecisionLogic PrecisionBack;
        public static PutOKNGLogic PutOKNGFront;
        public static PutScanNGLogic putScanNGLogicFront;
        public static PutScanNGLogic putScanNGLogicBack;
        public static PutOKNGLogic PutOKNGBack;
        public static RecycleTrayLogic RecycleTrayFront;
        public static RecycleTrayLogic RecycleTrayBack;
        public static RobotStatusLogic RobotStatusLogic1;
        public static RobotStatusLogic2 RobotStatusLogic2;
        public static RobotLogic RobotLogic1;
        public static RobotLogic RobotLogic2;
        public static List<TesterLogic> TestLogics = new List<TesterLogic>();
        public static List<TransferLogic> ArmLogics = new List<TransferLogic>();
        public static ClearBufferLogic clearLogicFront;
        public static ClearBufferLogic clearLogicBack;

        public static object RobotLock = new object();

        public static Tray BufferTray1UIFront;
        public static Tray BufferTray2UIFront;
        public static Tray BufferTray1UIBack;
        public static Tray BufferTray2UIBack;

        public static bool[] NeedOpenPrecisionFeedProductVacUp = new bool[2] { false, false };
        public static bool[] NeedClosePrecisionRecycleProductVacUp = new bool[2] { false, false };
        public static bool[] NozzleAlreadyLiftUp = new bool[2] { false, false };
        public static bool[] NeedOpenPrecisionFeedProductVacDown = new bool[2] { false, false };
        public static bool[] NeedClosePrecisionRecycleProductVacDown = new bool[2] { false, false };
        public static bool[] NozzleAlreadyLiftDown = new bool[2] { false, false };
        public static bool[] RobotZInNotSafeArea = new bool[2] { false, false };
        public static bool[] RobotCylinderWarn = new bool[2] { false, false };

        public static bool[] AlreadyOpenPrecisionFeedProductVacUp = new bool[2] { false, false };
        public static bool[] AlreadyClosePrecisionRecycleProductVacUp = new bool[2] { false, false };
        public static bool[] AlreadyGrabProductUp = new bool[2] { false, false };
        public static bool[] AlreadyOpenPrecisionFeedProductVacDown = new bool[2] { false, false };
        public static bool[] AlreadyClosePrecisionRecycleProductVacDown = new bool[2] { false, false };
        public static bool[] AlreadyGrabProductDown = new bool[2] { false, false };
        public static bool[] AlreadyRobotCylinderWarn = new bool[2] { false, false };

        public static bool[] ClearNG = new bool[4] { false, false, false, false };

        public static object TesterOKDataLock = new object();
        public static object TesterNGDataLock = new object();
        public static object UPHLock = new object();

        public static object FrontBackInputDataLock = new object();
        public static object FrontBackScanDataLock = new object();
        public static object FrontBackMesDataLock = new object();
        public static object FrontBackOutputDataLock = new object();

        public static Dictionary<string, int[]> SN2TestCount = new Dictionary<string, int[]>();
        public static Dictionary<string, bool[]> SN2FinishStatus = new Dictionary<string, bool[]>();
        public static object SNLock = new object();

        public static bool IsDryRun = false;
        public static bool IsSpotChecking = false;
        //public static CommunicationHelper TesterPC1;
        //public static CommunicationHelper TesterPC2;

        public static PointPath CopyPath;

        public static string Robot2Up1Barcode;
        public static string Robot2Up2Barcode;
        public static string Robot2Up5Barcode;
        public static string Robot2Up6Barcode;
        public static string Robot2Down1Barcode;
        public static string Robot2Down2Barcode;
        public static string Robot2Down5Barcode;
        public static string Robot2Down6Barcode;

        public static object ConveyorPLCLock = new object();

        public static string CurrentFrontOKBoatSN;
        public static string CurrentBackOKBoatSN;


        public static string CarrierCheckSN;

        public static bool IsSimulate = true;

        public static ModbusTcp ConveyorPLC;
        public static ModbusTcp FeedPLC;
        public static ModbusTcp Tester1PLC;
        public static ModbusTcp Tester2PLC;

        public static bool ChangeScanPosition = true;
        public static bool IsDownTimeFlag = false;
        public static Stopwatch DownTimeMonitor = new Stopwatch();
        public static bool IsJiaRateFlag = false;
        public static Stopwatch CTMachineMonitorFront = new Stopwatch();
        public static bool IsFristRunMachineFlagFront = false;
        public static double UPHFront = 0;
        public static double UPHBack = 0;
        public static Stopwatch CTMachineMonitorBack = new Stopwatch();
        public static bool IsFristRunMachineFlagBack = false;
        public static List<double> UPHListFront = new List<double>();
        public static List<double> UPHListBack = new List<double>();
        public static object _saveDataObj = new object();
        public static  Action OnSingleRunPause { get; set; }
        public static void SaveMachineData(MachineData data)
        {
            lock (_saveDataObj)
            {
                try
                {
                    XmlSerializerHelper<MachineData>.SerializeToFile(data, PathHelper.GetConfigPath("Machine") + "MachineData.xml");
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.Log("system", ex.Message, MessageLevel.Log);
                }
            }
        }

        public static short[] HandlerControlData = new short[2] { 0, 0 };
        public static short[] HandlerActionData = new short[2] { 0, 0 };
        public static short[] HandlerTesterCellData1 = new short[2] { 0, 0 };
        public static short[] HandlerTesterCellData2 = new short[2] { 0, 0 };
        public static short[] HandlerTesterCellData3 = new short[2] { 0, 0 };
        public static short[] HandlerTesterCellData4 = new short[2] { 0, 0 };
    }
    public enum WorkMode
    {
        Auto,
        GRR,
        Calibrate,
        Simulate,
        Retest
    }
    public enum WorkSpeed
    {
        Low,
        Medium,
        High
    }
}
